package com.leyou.user.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.NumberUtils;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.pojo.User;
import com.leyou.user.util.CodecUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    private final UserMapper userMapper;

    private static final String KEY_PREFIX = "user:verify:phone:";

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public Boolean checkData(String data, Integer type) {
        User record = new User();

        // 判断数据类型
        switch (type) {
            case 1:
                record.setUsername(data);
                break;
            case 2:
                record.setPhone(data);
                break;
            default:
                throw new LyException(ExceptionEnum.INVALID_USER_DATE_TYPE);
        }

        // 数据类型校验
        return userMapper.selectCount(record) == 0;
    }

    public void sendCode(String phone) {
        // 生成 key
        String key = KEY_PREFIX + phone;

        // 生成验证码
        String code = NumberUtils.generateCode(6);

        // 缓存验证码
        redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);

        // 数据封装
        Map<String, String> msg = new HashMap<>();
        msg.put("phone", phone);
        msg.put("code", code);

        // 异步发送验证码
        amqpTemplate.convertAndSend("ly.sms.exchange", "sms.verify.code", msg);
    }

    public void register(User user, String code) {
        // 校验验证码
        String key = KEY_PREFIX + user.getPhone();
        String checkCode = redisTemplate.opsForValue().get(key);

        if (!StringUtils.equals(code, checkCode)) {
            throw new LyException(ExceptionEnum.INVALID_VERIFY_CODE);
        }

        // 密码加密
        String salt = CodecUtils.generateSalt();

        user.setSalt(salt);
        user.setPassword(CodecUtils.md5Hex(user.getPassword(), salt));

        // 写入数据库
        user.setCreated(new Date());
        userMapper.insert(user);
    }

    public User queryUserBy(String username, String password) {
        User record = new User();
        record.setUsername(username);

        // 根据用户名查询用户信息
        User user = userMapper.selectOne(record);

        // 判断用户是否存在
        if (user == null) {
            throw new LyException(ExceptionEnum.INVALID_USERNAME_OR_PASSWORD);
        }
        // 加密
        password = CodecUtils.md5Hex(password, user.getSalt());

        // 比较密码
        if (!user.getPassword().equals(password)) {
            throw new LyException(ExceptionEnum.INVALID_USERNAME_OR_PASSWORD);
        }

        return user;
    }
}
